import Axios from '@/util/ajax'

const state = {
    //保存报价所有页面都会用到的一些值 ：如下拉框的列表值等,
    initPageData: {
        showSells: false, // 是否显示销售人员下拉框，管理者时需要
        customers: [], // 客户列表
        sells:[], // 销售人员
        units:[]  // 数量单位
    },
    showEditQuot: false,
    showQuotItemDialog: false,
    quotItems: [],
    quotId: -1,  // 新增或修改的报价
    quotItem: {},
    selectedItems: [],
    isView: false,
    isViewItem: false

};

// 通过 mutation 去更改 state 里面的值
const mutations = {
    Init_Page_Data: (state, data) => {
        state.initPageData = data;
    },
    Set_Show_Edit_Quot: (state, data) => {
        state.showEditQuot = data
    },
    Set_Quot_Id: (state, data) => {
        state.quotId = data
    },

    Show_Add_Quot_Item: (state) => {
        state.quotItem = {};
        state.quotItem.id = -1;
        state.quotItem.unit = "G";
        state.quotItem.quotId = state.quotId;
        state.showQuotItemDialog = true;
    },

    Show_Edit_Quot_Item: (state, quotItem) => {
        Object.assign(state.quotItem, quotItem);
        state.showQuotItemDialog = true;
        state.isViewItem = false;
    },

    showViewQuotItem: (state, quotItem) => {
        state.quotItem = quotItem;
        state.showQuotItemDialog = true;
        state.isViewItem = true;
    },

    Hide_Quot_Item_Dialog: (state) => {
        state.showQuotItemDialog = false
    },
    Show_Quot_Item_Dialog: (state) => {
        state.showQuotItemDialog = true
    },

    Set_Quot_Items: (state, data) => {
        state.quotItems = data
    },

    Set_Selected_Item: (state, data) => {
        state.selectedItems = data
    },

    Set_View_Model: (state, data) => {
        state.isView = data
    }
};

// 通过 action 去 commit mutation
const actions = {
    showEditQuot({commit}, quotId) {
        commit('Set_Show_Edit_Quot', true);
        commit('Set_Quot_Id', quotId);
        commit('Set_View_Model', false);
    },

    showViewQuot({commit}, quotId) {
        commit('Set_Show_Edit_Quot', true);
        commit('Set_Quot_Id', quotId);
        commit('Set_View_Model', true);
    },

    findQuotItemsByQuotId({state, commit}){
        Axios.get(`/sell/quot/quotItem/findQuotItemsByQuotId/${state.quotId}`).then((res) => {
            console.log("查询产品返回：", res);
            commit("Set_Quot_Items", res.result);

        })
    },

    removeQuotItems({state}){
        state.quotItems = state.quotItems.filter( it => {
            for(let i = 0; i < state.selectedItems.length; i++){
                if(state.selectedItems[i].id === it.id){
                    return false;
                }
            }
            return true;
        });
        console.log("state.quotItems", state.quotItems);
        state.selectedItems = [];
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
